package com.nconverter;

import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.media.MediaMetadataRetriever;
import android.os.Build;
import android.util.Log;

import java.io.IOException;
import java.util.Arrays;

public class Helper {
    public static void listEncoders() {

        MediaCodecList codecList = new MediaCodecList(1);
        for (MediaCodecInfo codecInfo : codecList.getCodecInfos()) {
            if (!codecInfo.isEncoder()) {
                continue;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                if (codecInfo.isAlias()) {
                    continue;
                }
                MyLog.i("", codecInfo.getName() + ", " + Arrays.toString(codecInfo.getSupportedTypes()) + codecInfo.isVendor() + codecInfo.isSoftwareOnly()
                        + codecInfo.isAlias() + codecInfo.isHardwareAccelerated());
            } else {
                MyLog.i("", codecInfo.getName() + ", " + Arrays.toString(codecInfo.getSupportedTypes()));
            }
        }
    }

    public static void listDecoders() {

        MediaCodecList codecList = new MediaCodecList(1);
        for (MediaCodecInfo codecInfo : codecList.getCodecInfos()) {
            if (codecInfo.isEncoder()) {
                continue;
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                if (codecInfo.isAlias()) {
                    continue;
                }
                MyLog.i("", codecInfo.getName() + ", " + Arrays.toString(codecInfo.getSupportedTypes()) + codecInfo.isVendor() + codecInfo.isSoftwareOnly()
                        + codecInfo.isAlias() + codecInfo.isHardwareAccelerated());
            } else {
                MyLog.i("", codecInfo.getName() + ", " + Arrays.toString(codecInfo.getSupportedTypes()));
            }
        }
    }

    public static void dumpinput(String input) {
        MediaMetadataRetriever mmr = new MediaMetadataRetriever();

        mmr.setDataSource(input);

//音频的数字描述
        String cd_track_number = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER);
// 专辑名
        String album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM);
// 艺术家
        String artist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST);
//作者
        String author = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_AUTHOR);
//作曲家
        String composer = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPOSER);
//创建或修改的日期
        String date = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE);
//内容的类型或流派
        String genre = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE);
//标题
        String title = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE);
//创建或修改的年份
        String year = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_YEAR);
//播放时长,毫秒
        String duration = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
//轨道数量
        String num_tracks = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_NUM_TRACKS);
//作者(比如歌词的作者)
        String writer = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_WRITER);
// 媒体格式
        String mimetype = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_MIMETYPE);
//演员或歌手
        String albumartist = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUMARTIST);
//描述音频来源的数字字符串
        String disc_number = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DISC_NUMBER);
//音乐专辑编辑状态
        String compilation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMPILATION);
//是否有音频
        String has_audio = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO);
//是否有视频
        String has_video = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
//视频宽度
        String video_width = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH);
//视频高度
        String video_height = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT);
//比特率,单位: bits/sec
        String bitrate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_BITRATE);
//文本轨道的同步语言,即 字幕
        String timed_text_languages = mmr.extractMetadata(21);
//Digital Rights Management 数字版权管理,是否被drm保护
        String is_drm = mmr.extractMetadata(22);
//位置信息
        String location = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_LOCATION);
//视频旋转角度
        String video_rotation = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION);
        //原始的捕捉帧速率,浮点型小数,require API 23
        String capture_framerate = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_CAPTURE_FRAMERATE);

        mmr.release();

        MyLog.i("", "-----------------start---------------------"
                + "音频的数字描述---cd_track_number: " + cd_track_number + "\n"
                + "专辑名---album: " + album + "\n"
                + "艺术家---artist: " + artist + "\n"
                + "作者---author: " + author + "\n"
                + "作曲---composer: " + composer + "\n"
                + "创建或修改的日期---date: " + date + "\n"
                + "内容的类型或流派---genre: " + genre + "\n"
                + "标---title:题" + title + "\n"
                + "创建或修改的年份---year: " + year + "\n"
                + "播放时长,毫秒---duration: " + duration + "\n"
                + "轨道数量---num_tracks: " + num_tracks + "\n"
                + "作者(比如歌词的作者)---writer: " + writer + "\n"
                + "媒体格式---mimetype: " + mimetype + "\n"
                + "演员或歌手---albumartist: " + albumartist + "\n"
                + "描述音频来源的数字字符串---disc_number: " + disc_number + "\n"
                + "音乐专辑编辑状态---compilation: " + compilation + "\n"
                + "是否有音频---has_audio: " + has_audio + "\n"
                + "是否有视频---has_video: " + has_video + "\n"
                + "视频宽度---video_width: " + video_width + "\n"
                + "视频高度---video_height: " + video_height + "\n"
                + "比特率,单位: bits/sec---bitrate: " + bitrate + "\n"
                + "文本轨道的同步语言,即 字幕---timed_text_languages: " + timed_text_languages + "\n"
                + "数字版权管理,Digital Rights Management,是否被drm保护---is_drm: " + is_drm + "\n"
                + "位置信息---location: " + location + "\n"
                + "视频旋转角度---video_rotation: " + video_rotation + "\n"
                + "原始的捕捉帧速率,浮点型小数,require API 23---capture_framerate: " + capture_framerate + "\n"
                + "-----------------end---------------------"
        );


        MediaExtractor ext = new MediaExtractor();
        try {
            ext.setDataSource(input);
            for(int i = 0; i < ext.getTrackCount(); i++){
                MediaFormat fmt = ext.getTrackFormat(i);
                MyLog.i("", "track1:" + fmt.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
